<?php include_once dirname(__FILE__).'/common/header.php'; ?>

<?php include_once dirname(__FILE__).'/common/menu.php'; ?>

var attr = <?php echo json_encode($attr) ?>;
var ctrlName = '<?php echo $ctrlName ?>';

attr.template_arr = [];
for(var i=0; i < attr.template_list.length; i++) {
  attr.template_arr.push([attr.template_list[i].template_id ,attr.template_list[i].template_name]);
}


list.baseFrm = function(r){
    var paramsData = r ? {activity_id:r.activity_id}: {};
    var win = new ZM.PopUp({
        title:r ? '编辑活动:' + r.activity_name : '新建活动',
        type:'s',
        items:new ZM.FormPanel({
            url:list.baseUrl +  ctrlName + (r ? '/editActivity' : '/createActivity'),
            params:paramsData,
            keySubmit:true,
            success:function(res) {
                ZM.bo.msg('success',res.detail);
                list.grid.store.reload();
                win.close();
            },
            items:[{
                fieldLabel:'名称',
                name:'activity_name',
                value: r ? r.activity_name : '',
                allowBlank:false
            },{
                fieldLabel:'活动url',
                name:'activity_url',
                value: r ? r.activity_url : '',
                allowBlank:false
            },{
                xtype:'combofield',
                fieldLabel:'红包模板',
                name:'template_id',
                value: r ? r.template_id : '',
                options: attr.template_arr,
                allowBlank:true
            }]
        })
    }).show();
}


list.statusFrm = function(r,msg,method){
    Ext.Msg.confirm('确认?',msg,function(btn) {
        if(btn != 'no') {
            ZM.ajax({
                url: list.baseUrl +  ctrlName + method,
                params:{customer_id:r.customer_id},
                mask:list.grid,
                success:function(res){
                    ZM.bo.msg('success!',res.detail);
                    list.grid.store.reload();
                }
            });
        }
    });
}


list.grid = new ZM.GridPanel({
    url: list.baseUrl + ctrlName + '/getActivityList',
    region: 'center',
    tbar:[{
      text:'新增活动',
      cls:'btn-show',
      handler:function() {
        list.baseFrm();
      }
    }],
    columns:[
        //{header:'ID',dataIndex:'customer_id',width: 60},
        {header:'识别码',dataIndex:'activity_code',width: 150},
        {header:'活动名称',dataIndex:'activity_name'},
        {header:'活动url',dataIndex:'activity_url',width: 200},
        {header:'模板',dataIndex:'template_id',width: 200,renderer:function(val) {
          for(var i=0,len = attr.template_list.length; i < len; i++) {
            var t = attr.template_list[i];
            if(t.template_id == val) {
              return t.template_name+'['+t.template_money+'元,'+t.template_support+']';
            }
          }
          return '';
        }},
        {header:'创建人',dataIndex:'user_name'},
        {header:'创建时间',dataIndex:'activity_time_create',toggle:true,width:130,filter:{xtype:'daterangefield'}},
        //{header:'最后操作时间',dataIndex:'customer_time_lastmodified',width:130},
        {header:'操作',renderer:function(v,p,r){
            var str = '';
            str += Ext.rd('编辑','fireEdit',r,ctrlName + '/editActivity');
      			str += Ext.rd('生成红包地址','fireMake',r,ctrlName + '/makeActivityUrl');
            return str;
        }}
    ]
});


list.grid.fireEdit = function(r) {
  list.baseFrm(r.json);
}

list.grid.fireMake = function(r) {
  r = r.json;
  ZM.ajax({
      url: list.baseUrl +  ctrlName + '/makeActivityUrl',
      params:{activity_id:r.activity_id},
      mask:list.grid,
      success:function(res){
        var w = new ZM.PopUp({
            title: '领奖url',
            type:'s',
            items:[{
              xtype:'textarea',
              value: res.addon.url,
              readOnly: true,
              id: 'texturl',
              listeners: {
                afterrender: function(cmp) {
                  setTimeout(function() {
                    Ext.getDom('texturl').select();
                  },100);
                }
              }
            }]
        }).show();
      }
  });
}


/*
list.grid.fireDetail = list.grid.fireHistory = function(r) {
  var r = r.json;
  list.detail.show().ownerCt.doLayout();
  list.history.baseParams = {'customer_id': r.customer_id};
  list.history.load();
}
*/

list.history = new ZM.EditorGridPanel({
  url: list.baseUrl + 'customer/getHistoryList',
  columns:[
    {header:'金额(元)',dataIndex:'history_money'},
    {header:'名称',dataIndex:'history_name',filter:{}},
    {header:'祝福语',dataIndex:'history_wish',id:'aeColumn',filter:{}},
    {header:'提供方名称',dataIndex:'history_support',width:150,filter:{}},
    {header:'状态',dataIndex:'history_status',width:65},
    {header:'返回结果',dataIndex:'history_result',width:150},
    {header:'最后修改时间',dataIndex:'history_time_lastmodified',width:140}
  ]
});

list.detail = new ZM.detailPanel({
  items: list.history
});



<?php include_once dirname(__FILE__).'/common/layout.php'; ?>
list.grid.store.load();
<?php include_once dirname(__FILE__).'/common/footer.php'; ?>
